/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __PQ_NativeFile_H__
#define __PQ_NativeFile_H__

#include <stdio.h>
#include<string>

#include "FileBase.h"
#include "DataSerializer.h"

namespace PQCore
{
	//NOTE:Simple C file wrap class
	//Also use SDTP to format data file
	//
	class NativeFile
		: public FileBase
	{
	public:
		NativeFile(std::string fileName);
		~NativeFile();

		inline bool ok()
		{
			return _ok;
		}

		static bool isExist(std::string fileName);
		//void close();//no open method because open at custruct
		//size_t read(void* dest,size_t size,size_t count);
		//size_t write(void* dest,size_t size,size_t count);

		size_t read(void* dest, size_t bytes);
		
		bool eof();
		int getSize();
		int getPos()
		{
			return _pos;
		}

		void* getBuffer()
		{
			return _buf;
		}

		static SerializeDatagram* readDatagram(std::string fileName);
		static void write(std::string fileName,SerializeDatagram* dgram);

		void seek(int offset)
		{
			_pos=offset;
		}

		int getPosition()
		{
			return _pos;
		}

		virtual void open()
		{
			_pos=0;
			_opened=true;
		}

		virtual void close()
		{
			_pos=0;
			_opened=false;

		}

		virtual void advance(int offset)
		{
			_pos+=offset;
		}

	protected:
		bool _ok;//is new created
	private:
		bool _eof;
		int _pos;
		int _len;
		void* _buf;
	};
}
#endif
